﻿'' GoogleCode - http://code.google.com/p/outlook-4-you/
'' GNU GPL v3 Licencing

Imports Microsoft.Office.Interop.Outlook
Imports System.Diagnostics

Public Class LeaveAddIn '' This plugin checks the calendar for changes to leave
#Region "Declarations"
#Region "UI declarations"
    Private WithEvents cb As Office.CommandBar
    Private WithEvents cbbLeave As Office.CommandBarButton
    Private Const cb_name As String = "Leave"
#End Region
#Region "Inspector UI"
    Private WithEvents cbt As Office.CommandBar
    Private WithEvents tbbCheck As Office.CommandBarButton
#End Region
#Region "Variable"
    Private WithEvents objApt As Outlook.AppointmentItem
    Private WithEvents objMail As Outlook.MailItem
    Private WithEvents objInspector As Outlook.Inspectors
    Private WithEvents CalItems, InboxItems, BuildItems As Outlook.Items
    Private olCal, olMail, olBuilds, olTasks As Outlook.Folder
    Private cLeave, pLeave, fLeave As Double
#End Region
#End Region

#Region "VSTO required"
    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        olCal = Me.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
        olMail = Me.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderInbox)
        olBuilds = olMail.Parent.folders("Builds")
        olTasks = Me.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderTasks)
        objInspector = Application.Inspectors
        AddHandler objInspector.NewInspector, AddressOf objInspector_NewInspector
        Add_Menubar()
        '' Add handlers for apps
        CalendarItem_Handler()
        '' Call button updates
        AL_Update()
    End Sub
    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub
#End Region

#Region "UI"
    Private Sub Add_Menubar()
        '' Setup Commandbar
        Try
            cb = Application.CommandBars(cb_name)
        Catch ex As MissingMemberException
            cb = Me.Application.ActiveExplorer.CommandBars.Add(cb_name, Office.MsoBarPosition.msoBarRight, False, True)
        End Try
        cb.Visible = True

        cbbLeave = cb.Controls.Add(Office.MsoControlType.msoControlButton, , , 1, True)
        With cbbLeave
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Leave"
            .FaceId = 643  '3197 file instack, 3975 linked comps, 0627 barcode, 2173 wand, 2950 smiley, 0643 POW
            .Tag = "css"
            .Visible = True
        End With
        AddHandler cbbLeave.Click, AddressOf Me.cbbLeave_Click
    End Sub    '' Creates button on commandbar
#End Region

#Region "Events"
    Private Sub cbbLeave_Click()
        AL_Update()
    End Sub
#End Region

#Region "Leave Specific code"
    Private Sub AL_Update()
        fLeave = 0
        cLeave = 0
        pLeave = 0
        Try
            CalItems = olCal.Items
            For Each Item In CalItems
                If (TypeOf Item Is Outlook.AppointmentItem) Then    '' Its an appointment?
                    objApt = TryCast(Item, Outlook.AppointmentItem)
                    If objApt IsNot Nothing Then    '' Cast worked?
                        If objApt.Subject.ToString.Contains("Annual Leave") = True Then
                            Dim FYa As Boolean = Check_year(objApt.Start)
                            Select Case FYa
                                Case True
                                    cLeave += DateDiff(DateInterval.Day, objApt.Start, objApt.End)
                                Case False

                                    If objApt.Start.Year < Now.Year Then
                                        pLeave += DateDiff(DateInterval.Day, objApt.Start, objApt.End)
                                    Else
                                        fLeave += DateDiff(DateInterval.Day, objApt.Start, objApt.End)
                                    End If
                            End Select
                        End If
                    End If
                End If
            Next
            cbbLeave.Caption = "Current:" & cLeave.ToString & " Future:" & fLeave.ToString
        Catch ex As System.Exception
            MsgBox("Unable to AddHandler to AppointmentItems in Folder_Handler" & vbCr & ex.Message.ToString, MsgBoxStyle.Information, "Appointment Monitor")
        End Try
    End Sub
    Private Function Check_year(ByVal dt As Date) As Boolean
        If dt.Year = Now.Year Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub CalendarItem_Handler()
        Try
            CalItems = olCal.Items
            For Each Item In CalItems
                If (TypeOf Item Is Outlook.AppointmentItem) Then    '' Its an appointment?
                    objApt = TryCast(Item, Outlook.AppointmentItem)
                    If objApt IsNot Nothing Then    '' Cast worked?
                        If objApt.Subject.ToString.Contains("Annual Leave") = True Then
                            AddHandler CalItems.ItemAdd, AddressOf AL_Update
                            AddHandler CalItems.ItemChange, AddressOf AL_Update
                            AddHandler CalItems.ItemRemove, AddressOf AL_Update
                        End If
                    End If
                End If
            Next
        Catch ex As System.Exception
            MsgBox("Unable to AddHandler to AppointmentItems in Folder_Handler" & vbCr & ex.Message.ToString, MsgBoxStyle.Information, "Appointment Monitor")
        End Try
    End Sub
#End Region

#Region "Outlook tasks"
    Private Sub objInspector_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector)
        Dim item As Object = Inspector.CurrentItem

        If (TypeOf item Is Outlook.TaskItem) Then
            Try
                objApt = TryCast(Inspector.CurrentItem, Outlook.AppointmentItem)
                If objApt IsNot Nothing Then
                    If objApt.Subject.ToString.Contains("Annual Leave") = True Then
                        AddHandler CalItems.ItemAdd, AddressOf AL_Update
                        AddHandler CalItems.ItemChange, AddressOf AL_Update
                        AddHandler CalItems.ItemRemove, AddressOf AL_Update
                    End If
                End If
            Catch ex As System.Exception
                MsgBox("Unable to AddHandler to AppointmentItem in Inspector" & vbCr & ex.Message.ToString, MsgBoxStyle.Information, "Appointment Monitor")
            End Try
        End If
    End Sub  '' Does something based on Inspector Type
    Public Shared Function Send_email(ByVal too As String, ByVal subj As String, ByVal body As String) As Boolean
        Dim request As Outlook.MailItem = Globals.LeaveAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem)
        Try
            With request
                .Subject = subj
                .Body = body
                .To = too
                .Importance = OlImportance.olImportanceHigh
                .Send()
            End With
            Return True
        Catch ex As SystemException
            MsgBox("Could not send email, Sorry")
            Return False
        End Try
    End Function
    Public Shared Function Create_build(ByVal ref As String, ByVal stream As String, ByVal sender As String, ByRef GUID As String) As Boolean
        Dim build As Outlook.TaskItem = Globals.LeaveAddIn.Application.CreateItem(Outlook.OlItemType.olTaskItem)

        Try
            Dim params As String() = stream.Split(",")

            Dim srcs As String = params(3).Replace("$$", vbCrLf & vbTab)
            Dim src As String = srcs.Replace("##", vbTab)

            Dim comps As String = params(4).Replace("$$", vbCr & vbTab)
            Dim comp As String = comps.Replace("##", vbTab)

            Dim sql As String = params(5).Replace("$$", vbCrLf)
            Dim imp As String = params(6).Replace("$$", vbCrLf)
            Dim info As String = params(7).Replace("$$", vbCrLf)

            Dim i As Integer = sender.LastIndexOf("CN=") + 3
            Dim sentname As String = sender.Substring(i, sender.Length - (i))
            Dim body As String = "Sender: " & sender & vbCrLf & _
                                 "ID: " & ref & vbCrLf & _
                                 "Product: " & params(0).ToString & vbCrLf & _
                                 "Release: " & params(1).ToString & vbCrLf & _
                                 "Description: " & params(2).ToString & vbCrLf & _
                                 "Sourcefiles:" & vbCrLf & vbTab & src & vbCrLf & _
                                 "Components: " & vbCr & vbTab & comp & vbCr & _
                                 "SQLs: " & vbCrLf & vbTab & sql & vbCrLf & _
                                 "Impact Analysis: " & vbCr & imp & vbCr & _
                                 "Additional Information: " & vbCr & info
            With build
                .Subject = "Build Request - " & sentname
                .Body = body
                .StartDate = Now
                .DueDate = Now.AddDays(7)
                .ReminderTime = Now.AddDays(7)
                .Status = OlTaskStatus.olTaskNotStarted
                .Importance = OlImportance.olImportanceHigh
                .Save()
            End With

            GUID = build.EntryID
            Return True
        Catch ex As SystemException
            MsgBox("Could not create build " & ref.Substring(Len(ref) - 10, 10) & ", Sorry" & vbCrLf & ex.Message)
            Return False
        End Try
    End Function
#End Region

#Region "Miscellaneous"
    Private Sub WeekStarts(ByRef Start As Date, ByRef dEnd As Date)
        Select Case DateAndTime.Now.DayOfWeek
            Case DayOfWeek.Monday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(6)
            Case DayOfWeek.Tuesday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 1, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(5)
            Case DayOfWeek.Wednesday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 2, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(4)
            Case DayOfWeek.Thursday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 3, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(3)
            Case DayOfWeek.Friday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 4, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(2)
            Case DayOfWeek.Saturday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 5, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(1)
            Case DayOfWeek.Sunday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 6, 0, 0, 0)
                dEnd = DateAndTime.Now
        End Select
    End Sub
#End Region
End Class
